package com.tzinfo.tzt.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;

import com.tzinfo.tzt.R;



public class DateTools {

	private Context context;

	public DateTools(Context context) {
		this.context = context;
	}

	public String getTimeSpacing(String time) {
		String timeSpacing = null;
		try {
			long now = System.currentTimeMillis();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			long millionSeconds = sdf.parse(time).getTime();
			long spacing = now - millionSeconds;
			if (spacing / 1000 < 60) {
				timeSpacing = spacing / 1000
						+ context.getResources().getString(R.string.before_sec);
			} else if (spacing / 1000 / 60 < 60) {
				timeSpacing = spacing / 1000 / 60
						+ context.getResources().getString(R.string.before_min);
			} else if (spacing / 1000 / 60 / 60 < 24) {
				timeSpacing = spacing / 1000 / 60 / 60
						+ context.getResources().getString(R.string.before_hou);
			} else if (spacing / 1000 / 60 / 60 / 24 < 3) {
				timeSpacing = spacing / 1000 / 60 / 60 / 24
						+ context.getResources().getString(R.string.before_day);
			} else {
				timeSpacing = time.substring(0, 16);
			}
		} catch (Exception e) {
			timeSpacing = time;
		}
		return timeSpacing;
	}

	/**
	 * get the movie end time
	 */
	public String movieEndTime(String showTime, String time) {
		int t1 = Integer.parseInt(showTime.substring(0, showTime.indexOf(":")));
		int t2 = Integer.parseInt(showTime.substring(showTime.indexOf(":") + 1,
				showTime.length()));
		int t3 = Integer.parseInt(time);
		String th, tm;
		t1 = t1 + t3 / 60;
		t2 = t2 + t3 % 60;
		for (; t2 >= 60;) {
			t1++;
			t2 = t2 - 60;
		}
		if (t1 < 10) {
			th = "0" + t1;
		} else {
			th = "" + t1;
		}
		if (t2 < 10) {
			tm = "0" + t2;
		} else {
			tm = "" + t2;
		}
		String endtime = String.format(
				context.getResources().getString(R.string.yuji_end_time), th
						+ ":" + tm);
		return endtime;
	}

	/**
	 * get the date 'today' to 'the day after tomorrow'
	 * 
	 * @author tongwanglin
	 * @param default return today，1 for tomorrow，2 for the ...
	 */
	public String getDate(int dayType) {
		long millis = System.currentTimeMillis();
		String year, month, day;
		Date date;
		Calendar cal;
		switch (dayType) {
		case 1:
			date = new Date(millis + 24 * 60 * 60 * 1000);
			cal = Calendar.getInstance();
			cal.setTime(date);
			year = cal.get(Calendar.YEAR) + "";
			if ((cal.get(Calendar.MONTH) + 1) < 10) {
				month = "0" + (cal.get(Calendar.MONTH) + 1);
			} else {
				month = "" + (cal.get(Calendar.MONTH) + 1);
			}
			if (cal.get(Calendar.DATE) < 10) {
				day = "0" + cal.get(Calendar.DATE);
			} else {
				day = "" + cal.get(Calendar.DATE);
			}
			break;
		case 2:
			date = new Date(millis + 24 * 60 * 60 * 1000 * 2);
			cal = Calendar.getInstance();
			cal.setTime(date);
			year = cal.get(Calendar.YEAR) + "";
			if ((cal.get(Calendar.MONTH) + 1) < 10) {
				month = "0" + (cal.get(Calendar.MONTH) + 1);
			} else {
				month = "" + (cal.get(Calendar.MONTH) + 1);
			}
			if (cal.get(Calendar.DATE) < 10) {
				day = "0" + cal.get(Calendar.DATE);
			} else {
				day = "" + cal.get(Calendar.DATE);
			}
			break;
		default:
			date = new Date(millis);
			cal = Calendar.getInstance();
			cal.setTime(date);
			year = cal.get(Calendar.YEAR) + "";
			if ((cal.get(Calendar.MONTH) + 1) < 10) {
				month = "0" + (cal.get(Calendar.MONTH) + 1);
			} else {
				month = "" + (cal.get(Calendar.MONTH) + 1);
			}
			if (cal.get(Calendar.DATE) < 10) {
				day = "0" + cal.get(Calendar.DATE);
			} else {
				day = "" + cal.get(Calendar.DATE);
			}
			break;
		}
		return year + "-" + month + "-" + day;
	}
}